package top.fzqblog.kotlinapp.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.Window
import android.view.WindowManager
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import top.fzqblog.kotlinapp.R
import top.fzqblog.kotlinapp.func.startActivity
import top.fzqblog.kotlinapp.func.toast
import top.fzqblog.kotlinapp.util.getUnionId
import top.fzqblog.kotlinapp.util.login

/**
 * Created by 抽离 on 2017/11/24 0024.
 */
class WelcomeActivity : Activity() {

    /**
     * 全屏设置
     */
    fun fullScreen() {
        //       设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        //        设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }

    private val handler: Handler = @SuppressLint("HandlerLeak")
    object : Handler() {
        override fun handleMessage(msg: Message?) {
                val unionId = getUnionId()
                    if (unionId.isNullOrEmpty()) {
                        startActivity<LoginActivity>()
                        finish()
                    }
                    else {
                        doAsync {
                            val flag = login()
                            uiThread {
                                if(flag){
                                    startActivity<MainActivity>()
                                    finish()
                                }
                                else{
                                    toast("登录失败！")
                                    startActivity<LoginActivity>()
                                    finish()
                                }
                            }
                        }
                    }

        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        fullScreen()
        setContentView(R.layout.activity_welcome)
        handler.sendEmptyMessageDelayed(0, 2000)
    }


    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }

}